package com.jewel.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jewel.mapper.TabUserMapper;
import com.jewel.pojo.TabUser;
import com.jewel.pojo.TabUserExample;
import com.jewel.service.LoginService;
import com.jewel.tools.CryptoUtils;
import com.jewel.tools.Tools;

import java.util.List;

/**
 * @Description: 登录模块业务类
 * 
 * @author DELL
 *
 * @since 2017.04.27
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private TabUserMapper tabUserMapper;


    /**
	 * @Title: loginIsSuccess
	 * @Description：登录成功
	 * 
	 * @param username， password
	 * @return boolean
	 * @throws：
	 * 
	 */
    @Override
    public boolean loginIsSuccess(String username, String password) {
        if(!Tools.objectIsNullOrNot(username) && !Tools.objectIsNullOrNot(password)){
            TabUser user = this.getUserByTelephone(username);

            if (!Tools.objectIsNullOrNot(user)) {
                if (CryptoUtils.verify(user.getPassword(), password, user.getSalt()))
                    return true;
            }
        }
        return false;
    }


    /**
	 * @Title: getUserByTelephone
	 * @Description：通过电话获取用户
	 * 
	 * @param telephone
	 * @return TabUser
	 * @throws：
	 * 
	 */
    @Override
    public TabUser getUserByTelephone(String telephone) {
        TabUserExample example = new TabUserExample();
        TabUserExample.Criteria criteria = example.createCriteria();
        criteria.andTelephoneEqualTo(telephone);

        List<TabUser> users = this.tabUserMapper.selectByExample(example);

        if (!Tools.objectIsNullOrNot(users))
            return users.get(0);
        else
            return null;
    }
}
